<template>
  <!-- 商品列表 -->
  <div class="goodsList">
    <!-- 排序 -->
    <div class="sortingBox">
      <van-tabbar
        v-model="active"
        active-color="#ee0a24"
        inactive-color="#000"
        class="sorting"
        @change="onChange"
      >
        <van-tabbar-item>
          <span>默认排序</span>
        </van-tabbar-item>
        <van-tabbar-item
          ><van-icon name="ascending" size="15px" /><span>升序</span>
        </van-tabbar-item>
        <van-tabbar-item
          ><van-icon name="descending" size="15px" />
          <span>降序</span></van-tabbar-item
        >
        <van-tabbar-item
          ><van-icon name="filter-o" size="15px" />
          <span>筛选</span></van-tabbar-item
        >
      </van-tabbar>
    </div>
    <!-- <lazy-component> -->
    <!-- 列表组件 -->
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="到头了到头了"
        loading-text="大风车吱呀吱呀转~~~"
        @load="onLoad"
      >
        <!-- 内容插槽 -->
        <template #default>
          <lazy-component>
            <!--  -->
            <van-card
              v-for="item in list"
              :key="item.id"
              :title="item.name"
              :desc="item.subtitle"
              :num="item.stock"
              :price="item.price"
              :origin-price="item.originalPrice"
              class="goodsInfo"
              @click="goDetgoods(item.id)"
            >
              <template #thumb>
                <img
                  alt=""
                  class="leftImg"
                  v-lazy="item.imageHost + item.mainImage"
                />
              </template>
            </van-card>
            <!--  -->
          </lazy-component>
        </template>
      </van-list>
    </van-pull-refresh>
    <!-- </lazy-component> -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      active: 0, // 排序列表数据
      listInfo: [],
      // 列表组件数据
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      ispageNum: 1,
    };
  },
  methods: {
    // 页面跳转
    goDetgoods(id) {
      this.$router.push(`/goodsEle${id}`);
    },
    // 列表组件方法
    onLoad() {
      setTimeout(() => {
        // this.finished = true;
        if (this.refreshing) {
          this.list = [];
          this.refreshing = false;
        }

        // 判断当前排序状态
        if (this.active === 0) {
          this.defaultSort(this.ispageNum);
        } else if (this.active === 1) {
          this.ascending(this.ispageNum);
        } else {
          this.descendingOrder(this.ispageNum);
        }
        this.ispageNum++;
        if (this.ispageNum > 4) {
          this.ispageNum = 1;
        }

        this.loading = false;
        // console.dir(window);

        if (this.list.length >= 20) {
          this.finished = true;
        }
      }, 1000);
    },
    onRefresh() {
      // 清空列表数据
      this.finished = false;

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true;
      this.onLoad();
    },
    // 监听切换
    onChange(index) {
      if (index == 0) {
        this.list = [];
        this.defaultSort();
      } else if (index == 1) {
        this.list = [];
        this.ascending();
      } else if (index == 2) {
        this.list = [];
        this.descendingOrder();
      } else {
        return;
      }
    },
    // 升序
    ascending(num = 1) {
      let info = {
        type: "get",
        url: `/product/list?orderBy=price_asc&pageNum=${num}&pageSize=10`,
      };
      this.$netWork(info).then((res) => {
        if (res.data.status === 0) {
          this.list.push(...res.data.data.list);
        }
      });
    },
    // 降序
    descendingOrder(num = 1) {
      let info = {
        type: "get",
        url: `/product/list?orderBy=price_desc&pageNum=${num}&pageSize=10`,
      };
      this.$netWork(info).then((res) => {
        if (res.data.status === 0) {
          this.list.push(...res.data.data.list);
        }
      });
    },
    // 默认排序
    defaultSort(num = 1) {
      // 全部商品
      let info = {
        type: "get",
        url: `/product/list?pageNum=${num}&pageSize=10`,
      };
      this.$netWork(info).then((res) => {
        if (res.data.status === 0) {
          this.list.push(...res.data.data.list);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.goodsList {
  padding-top: 115px;

  .goodsInfo {
    margin: 10px 0;
    text-align: left;
  }

  .leftImg {
    width: 88px;
    height: 88px;
  }
}
.sortingBox {
  position: relative;

  .sorting {
    position: fixed;
    top: 55px;
    height: 56px;
    border-top: 1.5px solid #dcdcdc;
    border-bottom: 1.5px solid #dcdcdc;
    span {
      font-size: 16px;
    }
  }
}
</style>
